const app = getApp();
var api = require('../../../api.js');
var mta = require('../../../mta_analysis.js');

Page({
  data: {

  },
  onLoad: function (options) {
    var page = this ;
    wx.getNetworkType({
      success: function (res) {
        console.log(res)
        page.setData({
          networkType: res.networkType
        })
      }
    });

    mta.Page.init();
  },
  onReady: function () {
    
  },
  onShow: function () {
    this.getCountDetail();
    
  },
  onHide: function () {
    
  },
  onUnload: function () {
    
  },
  onPullDownRefresh: function () {

    

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {
    
  },
  
  /**
   * 
   * 点击tabBar时触发
   * */
  onTabItemTap(item) {
    console.log(item.index)
    console.log(item.pagePath)
    console.log(item.text)
    console.log(222222222222222)
  },

  //获取未读消息详情
  getCountDetail: function () {
    var page = this;
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    wx.request({
      url: api.message.msg,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
           page.setData({
             count: res.data.data.count,
             list:res.data.data.list
           })

           if (res.data.data.count == 0){
             page.remove();
           }else{
             page.reseat();
           }
           

        } else if (res.data.code == 1) {



        } else if (res.data.code == -1) {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        }

      }
    });
  },
  
  //设置tabBar右上角数字
  reseat: function () {
    console.log(this.data.count)
    wx.setTabBarBadge({
      index: 2,
      text: this.data.count
    })
  },

  //移除tabBar右上角数字
  remove:function(){
    wx.removeTabBarBadge({
      index:2
    })
  },


  //去详情页面
  toDetail:function(res){
    console.log(res)
    var page = this;
    var index = res.currentTarget.dataset.index;
    var list = page.data.list;
    var message_id = list[index].message_id;
    var id = list[index].id
    console.log(message_id);
    wx.navigateTo({
      url: '/pages/news/newsSpecific/newsSpecific?message_id=' + message_id,
    });
    // 显示已读未读
    var page = this;
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    wx.request({
      url: api.message.read,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: id
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          

        } else if (res.data.code == 1) {


        } else if (res.data.code == -1) {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        }

      }
    });
  }

  
})